package View;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Controller.CtrlPwd;
import Listener.LIdentifyPwd;

public class ViewIdentify extends JDialog implements WindowListener {

	private static final long serialVersionUID = 1L;
	
	JPanel mainPanel;
	JPanel northPanel;
	JPanel southPanel;
	JPanel centerPanel;
	
	JPanel loginLabelPanel;
	JPanel pwdLabelPanel;
	JPanel loginTextPanel;
	JPanel pwdTextPanel;
	
	GridLayout centerLayout;
	
	JLabel title;
	JLabel loginLabel;
	JLabel pwdLabel;
	
	JTextField loginText;
	
	JPasswordField pwdText;
	
	JButton bOk;
	JButton bCancel;

	public ViewIdentify(CtrlPwd ctrl)
	{
		this.setBounds(400, 150, 600, 200);
		
		mainPanel = new JPanel();
		northPanel = new JPanel();
		southPanel = new JPanel();
		centerPanel = new JPanel();
		
		loginLabelPanel = new JPanel();
		pwdLabelPanel = new JPanel();
		loginTextPanel = new JPanel();
		pwdTextPanel = new JPanel();
		
		bOk = new JButton("Ok");
		bCancel = new JButton("Annuler");
		
		centerLayout = new GridLayout(2, 2);
		
		title = new JLabel("Identification");
		
		loginLabel = new JLabel("Login : ");
		loginText = new JTextField(30);
		
		pwdLabel = new JLabel("Password : ");
		pwdText = new JPasswordField(30);
		
		// Modifiying properties
		this.setResizable(false);
		
		centerPanel.setLayout(centerLayout);
		
		// Adding to Window
		loginLabelPanel.add(loginLabel);
		loginTextPanel.add(loginText);
		pwdLabelPanel.add(pwdLabel);
		pwdTextPanel.add(pwdText);
		
		northPanel.add(title);
		
		centerPanel.add(loginLabelPanel);
		centerPanel.add(loginTextPanel);
		centerPanel.add(pwdLabelPanel);
		centerPanel.add(pwdTextPanel);
		
		southPanel.add(bOk);
		southPanel.add(bCancel);
		
		this.add(northPanel, BorderLayout.NORTH);
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(southPanel, BorderLayout.SOUTH);

		bOk.addActionListener(new LIdentifyPwd(ctrl));
		bCancel.addActionListener(new LIdentifyPwd(ctrl));

		rootPane.setDefaultButton(bOk);
		
		this.pack();
	}
	
	public String getLogin()
	{
		return this.loginText.getText();
	}
	
	public String getPwd()
	{
		String pwd = new String(this.pwdText.getPassword());
		
		Arrays.fill(pwdText.getPassword(),'0');

		return pwd;
	}
	
	public void Close() 
	{	
		System.exit(0);
	}
	public void windowOpened(WindowEvent e) {
	}
	public void windowClosed(WindowEvent e) {
	}
	public void windowIconified(WindowEvent e) {
	}
	public void windowDeiconified(WindowEvent e) {
	}
	public void windowActivated(WindowEvent e) {
	}
	public void windowDeactivated(WindowEvent e) {
	}
	public void windowClosing(WindowEvent e)
	{
		this.Close();
	}
}
